﻿using GGetTestService.Services.Base;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;

namespace GGetTestService.Extension.ServiceExtensions
{
    public static class MyConfigServiceCollectionExtensions
    {
        /// <summary>
        /// 添加自定义服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddMyServiceConfig(this IServiceCollection services)
        {

            #region 从当前程序集中动态注入服务
            //var assembly = this.GetType().Assembly;
            var assembly = Assembly.GetExecutingAssembly();
            var types = assembly.GetTypes();
            foreach (var implement in assembly.GetTypes())
            {
                Type[] interfaceType = implement.GetInterfaces();
                foreach (var service in interfaceType)
                {
                    //只需要注册以Service结尾的服务
                    if (!string.IsNullOrEmpty(service.FullName) && implement.FullName.EndsWith("Service") && !implement.IsInterface)
                    {
                        services.AddScoped(service, implement);
                    }
                }
            }
            #endregion
            return services;
        }
    }
}
